/**
 * @CallFunctions.as
 * 
 * @author sodaChen mail:asframe@qq.com
 * @version 1.0
 * <br>Copyright (C), 2012 ASFrame.com
 * <br>This program is protected by copyright laws.
 * <br>Program Name:ASFrame EasyPlane
 * <br>Date:2012-11-5
 */
package com.asframe.fun
{
	import com.asframe.utils.VectorUtils;

	/**
	 * 对Function数组进行回调
	 * @author sodaChen
	 * Date:2012-11-5
	 */
	public class CallFunList
	{
		private var funList:Vector.<Function>;
		private var funCount:int;
		
		private var removeFunList:Vector.<Function>;
		private var addFunList:Vector.<Function>;
		
		private var isRunning:Boolean;
		
		private var tempFun:Function;
		
		public function CallFunList()
		{
			funList = new Vector.<Function>();
			removeFunList = new Vector.<Function>();
			addFunList = new Vector.<Function>();
		}
		public function addFunction(fun:Function):void
		{
			if(isRunning)
			{
				addFunList.push(fun);
				return ;
			}
			funList.push(fun);
			funCount++;
		}
		public function removeFunction(fun:Function):void
		{
			if(isRunning)
			{
				removeFunList.push(fun);
				return ;
			}
			VectorUtils.removeItem(funList,fun);
			funCount = funList.length;
		}
		public function clear():void
		{
			
		}
		public function calls():void
		{
			isRunning = true;
			for (var i:int = 0; i < funCount; i++) 
			{
				funList[i]();
			}
			isRunning = false;
			if(removeFunList.length > 0)
			{
				for each (tempFun in removeFunList) 
				{
					removeFunction(tempFun);
				}
				removeFunList.length = 0;
			}
			if(addFunList.length > 0)
			{
				for each (tempFun in addFunList) 
				{
					addFunction(tempFun);
				}
				addFunList.length = 0;
			}
		}
	}
}
